package project.dao.impl;

import Utils.db.dao.BaseDAO;
import project.dao.UserDAO;
import project.pojo.User;
import java.sql.SQLException;

/**
 * UserDAO的实例类
 *
 * @author d11yu
 */
public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public User queryUserByUsername(String username) throws SQLException {
        String sql = "select `id`, `username`, `password`, `email` from book.t_user where `username` = ?";
        return queryBean(User.class, sql, username);
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select `id`, `username`, `password`, `email` from book.t_user where `username` = ? and `password` = ?";
        return queryBean(User.class, sql, username, password);
    }

    @Override
    public User insertUser(User user) throws SQLException {
        String sql = "insert into t_user (`username`, `password`, `email`) values (?,?,?);";
        update(sql, user.getUsername(), user.getPassword(), user.getEmail());
        return queryUserByUsername(user.getUsername());
    }

    @Override
    public int deleteUserById(Integer id) {
        String sql = "delete from t_user where id = ?";
        return update(sql, id);
    }

    @Override
    public int deleteUserByUsername(String username) {
        String sql = "delete from t_user where username = ?";
        return update(sql, username);
    }
}
